package com.ulic.customerManApplication.tree;

import java.util.ArrayList;
import java.util.List;

public class Tree {
	private List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
	private TreeNode rootNode;
	
	public Tree(TreeNode rootNode){
		this.rootNode = rootNode;
		treeNodeList.add(rootNode);
	}
	
	public void clearSelectedState(){
		for(int i = 0; i < treeNodeList.size(); i++){
			TreeNode tn = treeNodeList.get(i);
			tn.setSelected(false);
		}
	}
	
	public TreeNode getNode(String code){
		for(int i = 0; i < treeNodeList.size(); i++){
			TreeNode tn = treeNodeList.get(i);
			if(tn.getCode().equals(code)){
				return tn;
			}
		}
		return null;
	}
	
	public void addNode(TreeNode treeNode){
		treeNodeList.add(treeNode);
		TreeNode parent = getNode(treeNode.getParentCode());
		treeNode.setParentNode(parent);
		parent.addChild(treeNode);
	}
	
	public TreeNode getRootNode() {
		return rootNode;
	}
}
